1650E - Rescheduling the Exam - CodeForces Solution


binary search data structures greedy implementation math sortings *1900

Please click on ads to support us..

Python Code:

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

t = int(input())
for tidx in range(t):
    input()
    n, d = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    b = [a[i+1]-a[i]-1 for i in range(n-1)]
    b.append(a[0]-1)
    mu = min(b)
    maxb = max((max(b)-1)//2, d-max(a)-1)
    idx = b.index(mu)     if idx == n-1:
                b[n-1] = d
        b[0] = a[1]-1
        maxb = max(maxb, (a[1]-2)//2)
        print(max(min(min(b), maxb), mu))
    else:
                b2 = b[:]
        if idx>0:
            c = a[idx+1]-a[idx-1]-1
            b[idx-1] = c
            b[idx] = d
        else: 
            c = a[1]-1
            b[n-1] = c
            b[idx] = d
        m = min(min(b), max(maxb, (c-1)//2))
        b = b2
                if idx<n-2:
            c = a[idx+2]-a[idx]-1
            b[idx] = c
            b[idx+1] = d
            m = max(m, min(min(b), max(maxb, (c-1)//2)))
        else: 
                        b[n-2] = d-a[n-2]-1
            m = max(m, min(b))
        print(max(m, mu))
        
        
    
    
    

C++ Code:

#include <bits/stdc++.h>

#define int long long
#define mp make_pair
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

typedef long double ld;
typedef long long ll;

using namespace std;

mt19937 rnd(143);

const ll inf = 1e9;
const ll M = 998'244'353;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;

int n, d;

int cnt(vector<int> &schedule){
    int mx = 0, mn = inf;
    for(int i = 1; i < n; ++i){
        mx = max(mx, schedule[i] - schedule[i - 1] - 1);
        mn = min(mn, schedule[i] - schedule[i - 1] - 1);
    }
    return min(mn, max(d - schedule.back() - 1, (mx - 1) / 2));
}

void solve(int test_case) {
    cin >> n >> d;
    vector<int> a(n + 1);
    int mn = d, min_pos = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(a[i] - a[i - 1] - 1 < mn){
            mn = a[i] - a[i - 1] - 1;
            min_pos = i;
        }
    }
    vector<int> schedule;
    for(int i = 0; i <= n; ++i){
        if(i != min_pos){
            schedule.push_back(a[i]);
        }
    }
    int ans = cnt(schedule);
    if(min_pos > 1){
        schedule[min_pos - 1] = a[min_pos];
    }
    ans = max(ans, cnt(schedule));
    cout << ans;
}

bool multi = true;

signed main() {
    //freopen("in.txt", "r", stdin);
    //freopen("in.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    if (multi) {
        cin >> t;
    }
    for (int i = 1; i <= t; ++i) {
        solve(i);
        cout << "\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle